package com.warmlight.voicepacket.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.warmlight.voicepacket.R;

/**
 * 作    者：MrYan （QQ：416643088）
 * 创建日期：7/11 0011.
 * 毕加索图片框架封装（Picasso）
 */

public class ImageLoader {


    /**
     * 默认图片
     */
    private static int defaultImg = R.drawable.video_icon;
    private static int defaultAvatar = R.drawable.video_icon;

    private static RequestOptions options;



    /**
     * 通过单例模式，初始化一个请求选项
     */
    private static synchronized RequestOptions getOptions() {
        if (options == null) {
            options = new RequestOptions().placeholder(defaultImg).error(defaultImg).dontAnimate();
        }
        return options;
    }


    /**
     * 小视频封面图片加载，动态修改填充方式
     */
    public static void smallVideoBackground(Context context, String url, final ImageView imageView) {
        if(ActivityUtil.checkActivityIsDetroy(context)){
            return;
        }
        if (TextUtils.isEmpty(url)) return;
        Glide.with(context).load(url).apply(getOptions()).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(final Drawable resource, Transition<? super Drawable> transition) {
                Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
                final int videoWidth = bitmap.getWidth();
                final int videoHeight = bitmap.getHeight();
                final float aspectRatio = (float) videoHeight / (float) videoWidth;
                imageView.post(new Runnable() {
                    @Override
                    public void run() {
                        if (aspectRatio > 1.5) {
                            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        } else {
                            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                        }
                        imageView.setImageDrawable(resource);
                    }
                });
            }
        });
    }
//

    public static void avatar(Context context, String url, ImageView img) {
        avatar(context, url, 200, 200, img);



    }

    /**
     * 注入变量
     *
     * @param context 上下文
     * @param url     图片Url
     * @param width   宽px
     * @param height  高px
     * @param img     ImageView
     */
    public static void avatar(Context context, String url, int width, int height, ImageView img) {
        if(ActivityUtil.checkActivityIsDetroy(context)){
            return;
        }
        RequestOptions options = new RequestOptions().placeholder(defaultAvatar).error(defaultAvatar).dontAnimate();
        if (!TextUtils.isEmpty(url)) {
            Glide.with(context).load(url).apply(options).into(img);
        } else {
            Glide.with(context).load(defaultAvatar).apply(options).into(img);
        }
    }

    public static void roundedCorners(Context context,String url,ImageView img){
        if(ActivityUtil.checkActivityIsDetroy(context)){
            return;
        }
        //设置图片圆角角度
        RoundedCorners roundedCorners= new RoundedCorners(8);
        //通过RequestOptions扩展功能
        RequestOptions options= RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(context).load(url).apply(options).into(img);
    }

    public static void intoRoundedCorners(Context context, int resourceId, ImageView img) {
        if(ActivityUtil.checkActivityIsDetroy(context)){
            return;
        }

        //设置图片圆角角度
        RoundedCorners roundedCorners= new RoundedCorners(8);
        //通过RequestOptions扩展功能
        RequestOptions options= RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(context).load(resourceId).apply(options).into(img);
    }

}
